package com.dkha.common.util;

import com.dkha.common.enums.ErrEnum;
import com.dkha.common.http.HttpUtil;
import com.dkha.common.modules.vo.ReturnVO;
import com.dkha.common.result.CommonResult;
import com.google.gson.Gson;

/**
 * @version V1.0
 * @Description: TODO(please write your description)
 * All rights 成都电科慧安
 * @Title: UrlUtil
 * @Package com.dkha.task.util
 * @author: panhui
 * @date: 2020/1/10 10:46
 * @Copyright: 成都电科慧安
 */
public class UrlUtil {
    /**
     * 调用预警的信息 faceserver
     *
     * @param object
     * @return
     */
    public static String postData(String warningUrl, Object object, HttpUtil httpUtil, Gson gson) {
        try {
            CommonResult commonResult = (CommonResult) httpUtil.post(warningUrl, object, CommonResult.class);
            if (commonResult.getCode().intValue() != 0 || UtilValidate.isEmpty(commonResult.getData())) {
                return null;
            }
            return gson.toJson(commonResult.getData());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 调用预警的信息 face-server
     *
     * @param warningUrl
     * @return
     */
    public static String getData(String warningUrl, HttpUtil httpUtil, Gson gson) {
//        log.error("warningUrl={}",warningUrl);
        try {

            CommonResult commonResult = (CommonResult) httpUtil.get(warningUrl, CommonResult.class);
            if (commonResult.getCode().intValue() != 0 || UtilValidate.isEmpty(commonResult.getData())) {
                return null;
            }
            return gson.toJson(commonResult.getData());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 调用 api
     *
     * @param object
     * @return
     */
    public static String postApiData(String warningUrl, Object object, HttpUtil httpUtil, Gson gson) {
        try {
            ReturnVO returnVO = (ReturnVO) httpUtil.post(warningUrl, object, ReturnVO.class);
            if (returnVO.getCode().intValue() != 200 || UtilValidate.isEmpty(returnVO.getData())) {
                return null;
            }
            return gson.toJson(returnVO.getData());
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 调用 api
     *
     * @param warningUrl
     * @return
     */
    public static String getApiData(String warningUrl, HttpUtil httpUtil, Gson gson) {
//        log.error("warningUrl={}",warningUrl);
        try {

            ReturnVO returnVO = (ReturnVO) httpUtil.get(warningUrl, ReturnVO.class);
            if (returnVO.getCode().intValue() != 200 || UtilValidate.isEmpty(returnVO.getData())) {
                return null;
            }
            return gson.toJson(returnVO.getData());
        } catch (Exception e) {
            return null;
        }
    }

    public static String getData(String warningUrl, Object object, HttpUtil httpUtil, Gson gson)
    {
        CommonResult commonResult= (CommonResult) httpUtil.get(warningUrl,object,CommonResult.class);
        //为授权
        if (commonResult.getCode().intValue()==ErrEnum.UNAUTHORIZED.getCode()){
            return   gson.toJson(401);
        }
        if(commonResult.getCode().intValue()!=0 || UtilValidate.isEmpty(commonResult.getData())) {
            return null;
        }
        return gson.toJson(commonResult.getData());
    }
}
